package com.test;

import java.sql.*;

/**
 * 电机故障分析
 */

public class JavaHive03 {
    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");

            String url = "jdbc:hive2://192.168.200.200:10000/test";

            connection = DriverManager.getConnection(url,"root","root");

            String sql = "select collect_set(error),component_number,count(component_number),error_type,count(error_type) from table_warning group by component_number,error_type";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
                System.out.println("The collect_set(error) is :   "+resultSet.getString(1)
                        +",    The component_number is : "+resultSet.getString(2)
                        +",   The count(component_number)num is: "+resultSet.getInt(3)
                        +",   The error_type is: "+resultSet.getString(4)
                        +",   The count(error_type) is:"+resultSet.getInt(5));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
